package com.tcu.mosaicdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 单块图片
 * Created by chenlong on 15/8/20.
 */
public class MosaicBlockView extends View {
    private static final String TAG = MosaicBlockView.class.getSimpleName();
    public  static final double WH_SCALE = 0.5625;
    private String mPath;
    private Context mContext;
    private Paint mPaint;
    private Bitmap bitmap;
    private Path mDPath;
    private int mHeight= 0;
    private int mWidth = 0;
    public MosaicBlockView(Context context) {
        super(context);
        init(context);
    }

    public MosaicBlockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MosaicBlockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        mContext = context;
        mPaint = new Paint();
    }

    @Override
    protected void onDetachedFromWindow() {
        if(bitmap!=null && !bitmap.isRecycled()){
            bitmap.recycle();
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mHeight = getHeight();
        mWidth = getWidth();
        Log.e(TAG, "H=" + mHeight + "W=" + mWidth);
        Rect rect = new Rect(0,0,mWidth,mHeight);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);
        mPaint.setAlpha(0);
        canvas.drawRect(rect,mPaint);
        Bitmap tp = createBitMap(bitmap);
        if(tp!=null){
            mPaint.setAlpha(255);
            canvas.drawBitmap(tp, 0, 0, mPaint);
        }
    }
    /**
    * 从原图计算出不规则图片
    **/
    private Bitmap createBitMap(Bitmap source){
        if(source == null){
            return null;
        }
        int w = mWidth;
        int h = mHeight;
        float wf = (float)mWidth/(float)source.getWidth();
        float hf = (float)mHeight/(float)source.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(wf,hf); //简单处理，把图片拉伸到和控件一样大之后，再计算
        Bitmap resizeBmp = Bitmap.createBitmap(source,0,0,source.getWidth(),source.getHeight(),matrix,true);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        canvas.drawPath(mDPath, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));//关键代码，请看 ‘实现思路.md’
        canvas.drawBitmap(resizeBmp, 0, 0, paint);
        return target;
    }



    /*
        Public Method
     */
    public void setPath(String path){
        mPath = path;
        bitmap = BitmapFactory.decodeFile(mPath);
    }
    public void setDrawPath(Path path){
        mDPath = path;
    }

}
